package com.sgb.kjwl.viewmodel.self;


import android.util.Log;
import android.widget.Toast;

import androidx.databinding.ObservableField;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.UserAuthMsgEntity;
import com.sgb.kjwl.model.entity.UserMsgEntity;
import com.sgb.kjwl.model.entity.user.CompanyInfoEntity;
import com.sgb.kjwl.repository.SelfRepertory;
import com.sgb.kjwl.view.ui.activity.self.ApplyToJoinTheEnterpriseActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.HashMap;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ApplyToJoinTheEnterpriseModel extends BaseViewModel {
    private ApplyToJoinTheEnterpriseActivity activity;
    private SelfRepertory repertory;
    private Map<String, Object> map = new HashMap<>();
    public ApplyToJoinTheEnterpriseModel(ApplyToJoinTheEnterpriseActivity activity, SelfRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }
    public void applyCompanyModel(String id,int type,String companyName, String companyNo,String realName,String reason,String idCard) {
        map.put("siteNo", companyNo);
        map.put("siteName", companyName);
        map.put("remark", reason);
        map.put("name", realName);
        map.put("identityCard", idCard);
        map.put("siteType", type);
        map.put("id", id);
        String aa =map.toString();
       Log.e("xx",aa);
        APIManager.getInstance().getUserAPI().applyOrganize(map).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                if (data.getState().equals(AppConstant.REQEUST_SECCUESS)) {
                    MToast.showToast(activity,"申请成功，请等待管理员审核");
                    activity.showApplyCompany();
                } else {
                    assert data != null;
                    MToast.showToast(activity, data.getMsg());
                }
            }
        });

    }
    public void getCompanyDetailModel(String companyNo) {
        APIManager.getInstance().getUserAPI().getCompanyDetailInfo(companyNo)
                .enqueue(new Callback<BaseEntity<CompanyInfoEntity>>() {
                    @Override
                    public void onResponse(Call<BaseEntity<CompanyInfoEntity>> call, Response<BaseEntity<CompanyInfoEntity>> response) {
                        if (response.body() == null || response.body().getState() == null) {
                            return;
                        }
                        if (response.body().getState().equals(AppConstant.REQEUST_SECCUESS)) {
                            activity.showCompanyDetai(response.body().getData());
                        } else {
                            assert response.body() != null;
                            MToast.showToast(activity, response.body().getMsg());
                        }
                    }

                    @Override
                    public void onFailure(Call<BaseEntity<CompanyInfoEntity>> call, Throwable t) {

                    }
                });
    }
    public void getUserInfo() {
        DialogHelper.showProgressDialog(activity, null, "请求中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUserAPI().getAuthenticationUserInfo().enqueue(new Callback<BaseEntity<UserAuthMsgEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<UserAuthMsgEntity>> call, Response<BaseEntity<UserAuthMsgEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    return;
                }
                if (response.body().getData() == null) return;
                if (response.body().getState().equals("ok")) {
                    activity.showAuth(response.body().getData());
                } else {
                    Toast.makeText(activity, response.body().getMsg(), Toast.LENGTH_LONG).show();
                }
            }
            @Override
            public void onFailure(Call<BaseEntity<UserAuthMsgEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
}
